package cn.pping.mq.producer;

import cn.pping.domain.MediaFile;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

@Component
public class MediaProducer {
    @Autowired
    public RocketMQTemplate rocketMQTemplate;

    // 生产者发送消息
    public boolean sendMesssge(MediaFile mediaFile){
        // 将传过来的对象转化为string
        String s = JSON.toJSONString(mediaFile);
        // 创建消息对象
        Message<String> build = MessageBuilder.withPayload(s).build();
        // 将这个对象发送同步消息
        SendResult sendResult = rocketMQTemplate.syncSend("topic-media:tag-media", build);
        SendStatus sendStatus = sendResult.getSendStatus();
        return sendStatus.equals(sendResult);
    }

}
